package pl.edu.agh.jemo.gui.panels.selection;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import pl.edu.agh.jemo.evolution.selections.impl.ClassicTournament;

/**
 * Configuration panel allowing to adjust classic tournament parameters.
 * As an extension of JPanel, it can be displayed by main window, whenever it is needed.
 * It works on existing function instance, using reference passed in constructor.
 * 
 * @author Marcin Zbijowski
 *
 */
public class ClassicTournamentConfigPanel extends TournamentConfigPanel {
	/**
	 * As JPanel is implementation of Serializable interface, it requires this field to be set.
	 */
	private static final long serialVersionUID = 7783015028383924070L;

	/**
	 * Reference to tournament instance configured by panel.
	 */
	protected final ClassicTournament classicTournament;

	/**
	 * Field allowing to set number of specimen chosen for single tournament.
	 */
	protected JTextField tournamentSizeField;
	/**
	 * Field allowing to set probability of chosing the best specimen from single tournament.
	 */
	protected JTextField probabilityField;

	/**
	 * Creates Panel instance associated with given tournament reference.
	 * Initializes all graphic components, so after constructor no further initialization is needed.
	 * 
	 * @param tournamentRef reference to classic tournament, that needs configuration. 
	 */
	public ClassicTournamentConfigPanel(ClassicTournament tournamentRef) {
		super(tournamentRef);
		classicTournament = tournamentRef;

		setLayout(null);

		JLabel archiveLabel = new JLabel("Tournament size:");
		archiveLabel.setBounds(10, 45, 150, 15);
		add(archiveLabel);

		tournamentSizeField = new JTextField(String.valueOf(classicTournament.getTournamentSize()));
		tournamentSizeField.setBounds(10, 62, 150, 22);
		add(tournamentSizeField);

		tournamentSizeField.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				try {
					classicTournament.setTournamentSizeRatio(Double.parseDouble(tournamentSizeField
							.getText()));
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Wrong value");
				}
			}
		});

		JLabel chanceLabel = new JLabel("Probability:");
		chanceLabel.setBounds(10, 90, 150, 15);
		add(chanceLabel);

		probabilityField = new JTextField(String.valueOf(classicTournament
				.getProbability()));
		probabilityField.setBounds(10, 107, 150, 22);
		add(probabilityField);

		probabilityField.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				try {
					classicTournament.setProbability(Double
							.parseDouble(probabilityField.getText()));
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Wrong double value");
				}
			}
		});
	}
}
